package com.kotlin.user.ui.activity;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.kotlin.user.R;

public class Main2Activity extends AppCompatActivity implements SurfaceHolder.Callback{

    SurfaceHolder holder;
    SurfaceView surfaceView;
    boolean isSurfaceCreated = false;
    int left,top,right,bootom,width,height;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        surfaceView = findViewById(R.id.mSurfaceView);
        findViewById(R.id.mBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new  MyThread().start();
            }
        });
        holder = surfaceView.getHolder();
        holder.addCallback(this);

//
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        isSurfaceCreated = true;
        left = surfaceView.getLeft();
        top = surfaceView.getTop();
        right = surfaceView.getRight();
        bootom = surfaceView.getBottom();
        width = surfaceView.getWidth();
        height = surfaceView.getHeight();
        Log.d("---","left = "+left+" top = "+top+" right = "+right+" bootom = "+bootom);
        Log.d("---","width = "+width+" height = "+height);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    private class MyThread extends Thread{
        private boolean isDrawing = true;
        private Paint paint = new Paint();
        RectF rectF = new RectF(20,20,right-left-20,bootom-top-20);
        int num = 360;
        int current =0;
        @Override
        public void run() {
            Log.d("---","run");
            super.run();
            Canvas canvas = null;
            while (isDrawing){
                Log.d("---","isDrawing");
                canvas = holder.lockCanvas();//
                canvas.drawColor(Color.WHITE);
                paint.setAntiAlias(true);
                paint.setStrokeWidth(12.0f);
                paint.setStyle(Paint.Style.STROKE);
                paint.setColor(Color.rgb(0xe9, 0xe9, 0xe9));

                canvas.drawArc(rectF,-90,360,false,paint);//-90表示从竖直上的方向开始顺时针绘制 第三个参数false 画弧度的时候 边框不显示

                paint.setColor(Color.rgb(0xf8, 0x60, 0x30));
                canvas.drawArc(rectF,-90,current,false,paint);//

                paint.setStyle(Paint.Style.FILL);
                paint.setStrokeWidth(1.0f);
                paint.setTextSize(50);
                paint.setColor(Color.BLUE);
                paint.setTextAlign(Paint.Align.CENTER);
                float baseX = width/2;
                float baseY = height/2;
                canvas.drawText((current*100)/360+"%",baseX,baseY,paint);
                try {
                    if(current >=360){
                        isDrawing =false;
                        current = 360;
                    }else{
                        current = current+10;
                    }
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                holder.unlockCanvasAndPost(canvas);
            }
            isDrawing = true;
        }
    }
}
